package com.acwing.partition2;

import java.io.*;
import java.util.*;

/**
 * @author `RKC`
 * @date 2022/1/4 14:50
 */
public class AC180排书aStar {

    private static final int N = 20, P = 131;
    private static int n = 0;
    private static final int[] nums = new int[N];

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        int k = Integer.parseInt(reader.readLine());
        while (k-- > 0) {
            n = Integer.parseInt(reader.readLine());
            String[] s = reader.readLine().split("\\s+");
            for (int i = 0; i < n; i++) nums[i] = Integer.parseInt(s[i]);
            int ans = aStar();
            if (ans == -1) writer.write("5 or more\n");
            else writer.write(ans + "\n");
        }
        writer.flush();
    }

    private static int aStar() {
        //TODO 通过数据4/7，TLE
        Map<Long, Integer> distMap = new HashMap<>();
        long h = numsHash(nums);
        distMap.put(h, 0);
        PriorityQueue<Node> heap = new PriorityQueue<>((o1, o2) -> Integer.compare(o1.val, o2.val));
        heap.add(new Node(eval(nums), h, nums));
        while (!heap.isEmpty()) {
            Node curr = heap.poll();
            int[] nums = curr.nums;
            int dist = distMap.get(curr.hashKey);
            if (dist >= 5) return -1;
            //如果估价值为0，说明找到了最终的状态
            if (eval(nums) == 0) return dist;
            //枚举当前状态所有区间插入的方式
            for (int length = 1; length <= n; length++) {
                for (int i = 0; i + length - 1 < n; i++) {
                    int j = i + length - 1;
                    for (int k = 0; k < n; k++) {
                        int[] nextNums = intervalInsertTo(nums, i, j, k);
                        //得到下一个状态的哈希值，用于在Map中取得步数
                        h = numsHash(nextNums);
                        if (distMap.containsKey(h)) continue;
                        heap.add(new Node(eval(nextNums) + dist + 1, h, nextNums));
                        distMap.put(h, dist + 1);
                        if (k == i) k = j;
                    }
                }
            }
        }
        return -1;
    }

    private static int[] intervalInsertTo(int[] nums, int left, int right, int k) {
        //将nums的区间[left,right]插入到k后面并返回新数组，不在原数组上做改动
        int[] result = new int[nums.length];
        System.arraycopy(nums, 0, result, 0, nums.length);
        if (k < left) {
            int j = k + 1;
            for (int i = left; i <= right; i++, j++) result[j] = nums[i];
            for (int i = k + 1; i < left; i++, j++) result[j] = nums[i];
        } else if (k > right) {
            int j = left;
            for (int i = right + 1; i <= k; i++, j++) result[j] = nums[i];
            for (int i = left; i <= right; i++, j++) result[j] = nums[i];
        }
        return result;
    }

    private static int eval(int[] nums) {
        //得到当前状态的估价值
        int res = 0;
        for (int i = 0; i + 1 < n; i++) {
            if (nums[i + 1] != nums[i] + 1) res++;
        }
        return (res + 2) / 3;
    }

    private static long numsHash(int[] nums) {
        //字符串哈希的方式对序列进行hash，如果直接把nums的字符序列存储在Set或者Map中，第一个样例都会超时
        long[] h = new long[n + 1];
        for (int i = 1; i <= n; i++) h[i] = h[i - 1] * P + nums[i - 1];
        return h[n];
    }

    private static class Node {
        private int val;
        private int[] nums;
        private long hashKey;

        public Node(int val, long hashKey, int[] nums) {
            this.val = val;
            this.nums = nums;
            this.hashKey = hashKey;
        }
    }
}
